package com.uzai.console.config;

import com.alipay.api.CertAlipayRequest;
import com.taobao.api.internal.toplink.embedded.websocket.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * TODO 支付宝配置文件
 *
 * @author liuqi
 * @date 2020/2/9 20:15
 */
@Component
@RefreshScope
public class AliPayConfig {

    private   String APP_ID="2018121562XXXXX";
    //商户私钥，您的PKCS8格式RSA2私钥
    private  String MERCHANT_PRIVATE_KEY="MIIEvQIBADANxxxxxxxxxxxxx";
    //支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiGxxxxxxxxxxxxxxxxx";
    //服务器异步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    private  String NOTIFY_URL ="/console/alipay/notify";
    //页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    private  String RETURN_URL = "/console/alipay/return";
    //签名方式
    private  String SIGN_TYPE="RSA2";
    //字符编码格式
    private  String CHARSET="utf-8";
    //支付宝网关(沙盒环境)
    private  String GATEWAY_URL="https://openapi.alipay.com/gateway.do";
    //参数格式
    private  String Format="json";
    //读取证书文件目录
    private  String ALIPAYCRT_PATH =  "/servers/alipaycrt/";
    //Cert文件名
    private  String CERT_NAME = "apppubcert.crt";
    //公钥文件名
    private  String PUBLIC_CERT = "alipayCertPublicKey_RSA2.crt";
    //根钥文件名
    private  String ROOT_CERT = "alipayRootCert.crt";

    public  CertAlipayRequest getCertAlipayRequest(String aliPayAppId, String aliPayAppPriKey){

        try{

            //读取证书文件
            String alipaycrt_path = ALIPAYCRT_PATH + File.separator ; //默认linux

            //检查系统是否为window系统
            String osName = System.getProperty("os.name");
            if(StringUtils.isNotBlank(osName)){
                if(osName.contains("indow")){
                    alipaycrt_path =  ResourceUtils.getURL("classpath:").getPath() + "alipaycrt" + File.separator;
                    alipaycrt_path = alipaycrt_path.replace("/D:/","D:/");
                }
            }

            //构造client(证书访问)
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
            certAlipayRequest.setAppId(aliPayAppId);
            certAlipayRequest.setPrivateKey(aliPayAppPriKey);
            certAlipayRequest.setServerUrl(GATEWAY_URL);
            certAlipayRequest.setFormat(Format);
            certAlipayRequest.setCharset(CHARSET);
            certAlipayRequest.setSignType(SIGN_TYPE);
            certAlipayRequest.setCertPath(alipaycrt_path + CERT_NAME);
            certAlipayRequest.setAlipayPublicCertPath(alipaycrt_path + PUBLIC_CERT);
            certAlipayRequest.setRootCertPath(alipaycrt_path + ROOT_CERT);
            return certAlipayRequest;
        } catch (Exception e){
            return null;
        }
    }

    public String getAPP_ID() {
        return APP_ID;
    }

    public String getMERCHANT_PRIVATE_KEY() {
        return MERCHANT_PRIVATE_KEY;
    }

    public String getALIPAY_PUBLIC_KEY() {
        return ALIPAY_PUBLIC_KEY;
    }

    public String getNOTIFY_URL() {
        return NOTIFY_URL;
    }

    public String getRETURN_URL() {
        return RETURN_URL;
    }

    public String getSIGN_TYPE() {
        return SIGN_TYPE;
    }

    public String getCHARSET() {
        return CHARSET;
    }

    public String getGATEWAY_URL() {
        return GATEWAY_URL;
    }

    public String getFormat() {
        return Format;
    }

    public String getALIPAYCRT_PATH() {
        return ALIPAYCRT_PATH;
    }

    public String getCERT_NAME() {
        return CERT_NAME;
    }

    public String getPUBLIC_CERT() {
        return PUBLIC_CERT;
    }

    public String getROOT_CERT() {
        return ROOT_CERT;
    }
}
